package com.xhsj.user.hr.process.tbLearningGarden.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.process.dto.tbLearningGarden.*;
import com.xhsj.user.hr.process.message.service.TbProjectMessageService;
import com.xhsj.user.hr.process.query.ApprovalQuery;
import com.xhsj.user.hr.process.tbLearningGarden.entity.TbLearningGardenContentApply;
import com.xhsj.user.hr.process.tbLearningGarden.query.*;
import com.xhsj.user.hr.process.tbLearningGarden.query.FindUnReadListQuery;
import com.xhsj.user.hr.process.tbLearningGarden.query.GetLearningGardenContentApplyDetailQuery;
import com.xhsj.user.hr.process.tbLearningGarden.query.MyLearningGardenContentApplyListQuery;
import com.xhsj.user.hr.process.tbLearningGarden.query.RunLearningGardenContentApplyListQuery;
import com.xhsj.user.hr.process.tbLearningGarden.service.TbLearningGardenContentApplyService;
import com.xhsj.user.hr.process.tbLearningGarden.vo.FindUnReadListVo;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * <p>
 * 学习园地-内容 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Nov 20, 2020 10:59:29 AM
 */
@Slf4j
@Api(tags = "学习园地-内容")
@RestController
@RequestMapping("/tbLearningGarden/tbLearningGardenContent")
public class TbLearningGardenContentApplyController {

	@Autowired
    public TbLearningGardenContentApplyService service;

    // 项目消息 服务实现类
    @Autowired
    public TbProjectMessageService tbProjectMessageService;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Nov 20, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "学习园地-内容,保存/修改", notes = "学习园地-内容,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:edit')")
	public Message save(@RequestBody TbLearningGardenContentApply data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Nov 20, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "学习园地-内容删除", notes = "通过id删除学习园地-内容信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Nov 20, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:learning:garden:content:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Nov 20, 2020 10:59:29 AM
    */
	@ApiOperation(value = "分页查询学习园地-内容list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
//    @PreAuthorize("hasAuthority('tb:learning:garden:content:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbLearningGardenContentApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Nov 20, 2020 10:59:29 AM
    */
	@ApiOperation(value = "查询学习园地-内容所有数据list", notes = "查询学习园地-内容所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Nov 20, 2020 10:59:29 AM
    */
	@ApiOperation(value = "通过id获取学习园地-内容实体类", notes = "通过id获取学习园地-内容实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Nov 20, 2020 10:59:29 AM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}


	/**
	 * 学习园地-内容 保存/发布
	 * @author liuqinghua
	 * @date Nov 20, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "学习园地-内容,保存/发布", notes = "学习园地-内容,保存/发布方法")
	@RequestMapping(value = "/savePush", method = RequestMethod.POST)
	public Message savePush(@RequestBody TbLearningGardenContentDto data){
		try {

			ApprovalQuery approvalQuery = service.savePush(data);
			//保存消息
			CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				tbProjectMessageService.sendMessageCommon(approvalQuery);
			});
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}

	   /**
	       * @Description:  学习园地-内容重新提交
	       * @Author: liuqinghua
	       * @Date: 2020/11/25
	       */
	   @ApiOperation(value = "学习园地-内容重新提交", notes = "学习园地-内容重新提交")
	   @RequestMapping(value = "/returnPush", method = RequestMethod.POST)
	   public Message returnPush(@RequestBody ReturnPushDto data){
		   try {
			   ApprovalQuery approvalQuery = service.returnPush(data);
			   //保存消息
			   CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				   tbProjectMessageService.sendMessageCommon(approvalQuery);
			   });
			   return Message.success("成功");
		   } catch (Exception e) {
			   log.error("保存失败:{}",e);
			   return Message.error(1,"失败");
		   }
	   }

	/**
	 * 学习园地-内容审核
	 * @author liuqinghua
	 * @date Nov 23, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "学习园地-内容审核", notes = "学习园地-内容审核")
	@RequestMapping(value = "/approvalLearningGardenContentApply", method = RequestMethod.POST)
	public Message approvalLearningGardenContentApply(@RequestBody ApprovalLearningGardenContentApplyDto data){
		try {
			ApprovalQuery approvalQuery = service.approvalLearningGardenContentApply(data);
			//保存消息
			CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				tbProjectMessageService.sendMessageCommon(approvalQuery);
			});
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,"失败");
		}

	}

	/**
	 * 我的学习园地内容申请列表
	 * @author liuqinghua
	 * @date Nov 23, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "我的学习园地内容申请列表", notes = "我的学习园地内容申请列表")
	@PostMapping(value="/myLearningGardenContentApplyList")
	public Message myLearningGardenContentApplyList(@RequestBody @Valid MyLearningGardenContentApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("我的学习园地内容申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<MyLearningGardenContentApplyListQuery> myTransferJobApplyListQueryPage = service.myLearningGardenContentApplyList(data);
		return Message.success("成功",myTransferJobApplyListQueryPage.getRecords(),myTransferJobApplyListQueryPage.getTotal(),myTransferJobApplyListQueryPage.getPages());
	}

	/**
	 * 待审核学习园地内容申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "待审核学习园地内容申请列表", notes = "待审核学习园地内容申请列表")
	@PostMapping(value="/runLearningGardenContentApplyList")
	public Message runLearningGardenContentApplyList(@RequestBody @Valid RunLearningGardenContentApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("待审核学习园地内容申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<RunLearningGardenContentApplyListQuery> myTransferJobApplyListQueryPage = service.runLearningGardenContentApplyList(data);
		return Message.success("成功",myTransferJobApplyListQueryPage.getRecords(),myTransferJobApplyListQueryPage.getTotal(),myTransferJobApplyListQueryPage.getPages());
	}

	   /**
	       * @Description:学习园地内容详情
	       * @Param:dataId
	       * @Author: liuqinghua
	       * @Date: 2020/11/23
	       */
	   @ApiOperation(value = "学习园地内容详情", notes = "学习园地内容详情")
	   @GetMapping(value="/getLearningGardenContentApplyDetail")
	   public Message getLearningGardenContentApplyDetail(@RequestParam(value = "dataId") Long dataId) throws Exception {
		   log.info("学习园地内容详情参数{}", dataId);

		   GetLearningGardenContentApplyDetailQuery myTransferJobApplyListQueryPage = service.getLearningGardenContentApplyDetail(dataId);
		   return Message.success("成功",myTransferJobApplyListQueryPage);
	   }

	/**
	 * 学习园地-内容 删除
	 * @author liuqinghua
	 * @date Nov 20, 2020 10:59:29 AM
	 */
	@ApiOperation(value = "学习园地-内容删除", notes = "学习园地-内容删除")
	@RequestMapping(value = "/deletePush", method = RequestMethod.GET)
	public Message deletePush(@RequestParam(value = "id") Integer id){
		try {
			service.deletePush(id);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
			return Message.error(1,"失败");
		}
	}


	/**
	 * 查询未阅读信息列表
	 * @author 苏小林
	 * @date 2020/11/23 14:42
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询未阅读信息列表", notes = "查询未阅读信息列表")
	@RequestMapping(value = "/findUnReadListByUserId", method = RequestMethod.POST)
	public Message findUnReadListByUserId(@RequestBody FindUnReadListQuery baseParam) {
		try {

			IPage<FindUnReadListVo> selectPage = service.findUnReadListByUserId(baseParam);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("删除失败:{}",e);
			return Message.error(1,"失败");
		}
	}

	   /**
	       * @Description:  已审核的学习园地内容申请列表
	       * @Author: liuqinghua
	       * @Date: 2020/11/24
	       */
	   @ApiOperation(value = "已审核的学习园地内容申请列表", notes = "已审核的学习园地内容申请列表")
	   @PostMapping(value="/completedLearningGardenContentApplyList")
	   public Message completedLearningGardenContentApplyList(@RequestBody @Valid RunLearningGardenContentApplyListDto data, BindingResult bindingResult){
		   log.info("已审核的学习园地内容申请列表参数{}", data);
		   if (bindingResult.hasErrors()) {
			   log.info(bindingResult.getFieldError().getDefaultMessage());
			   return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		   }
		   IPage<CompletedLearningGardenContentApplyQuery> completedTransferJobApplyList = service.completedLearningGardenContentApplyList(data);
		   return Message.success("成功",completedTransferJobApplyList.getRecords(),completedTransferJobApplyList.getTotal(),completedTransferJobApplyList.getPages());
	   }

}

